package com.qf.produce.service;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * description
 *
 * @author TangLin
 * @date 2020年08月10日 16:19
 */
@Service
public class TestService {

    @Resource
    private AmqpTemplate amqpTemplate;

    public void sendMsg(String msg){
//        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
//            @Override
//            public Message postProcessMessage(Message message) throws AmqpException {
//                message.getMessageProperties().setHeader("ta", "测试");
//                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
//                return message;
//            }
//        };
//        //3. 发送消息到交换机(路由交换机)
//        amqpTemplate.convertAndSend("ex1","k1",msg,messagePostProcessor);
                amqpTemplate.convertAndSend("ex1","k1",msg);

    }

    public void sendMsg2(String msg){
        //3. 发送消息到交换机(路由交换机)
        amqpTemplate.convertAndSend("ex1","k3",msg);

    }
    //幂等性消费问题
    public void sendMsg3(String msg){
       msg = msg + System.currentTimeMillis();
        //请求头设置消息id（messageId）
        Message message = MessageBuilder.withBody(msg.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8").setMessageId(UUID.randomUUID() + "").build();
        System.out.println(msg + ":" + msg);
        //3. 发送消息到交换机(路由交换机)
        amqpTemplate.convertAndSend("ex1","k1",message);

    }
    //幂等性消费问题
    public void sendMsg4(String msg){
        msg = msg + System.currentTimeMillis();
        //请求头设置消息id（messageId）
        Message message = MessageBuilder.withBody(msg.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8").setMessageId("4241dd86-dc69-4678-9abb-340885fd17b8").build();
        System.out.println(msg + ":" + msg);
        //3. 发送消息到交换机(路由交换机)
        amqpTemplate.convertAndSend("ex1","k1",message);

    }
}
